<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_from" :rules="rules" :model="loginForm" ref="logRef">
                    <h1>Hello</h1>
                    <h2>欢迎来到vue3管理系统</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="pass  word" :prefix-icon="Lock" v-model="loginForm.password"
                            show-password></el-input>
                    </el-form-item>
                    <el-button :loading="loading" type="primary" size="default" class="login_btn"
                        @click="login">登录</el-button>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script lang='ts' setup>
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ElNotification } from "element-plus";
import { getTime } from '@/utils/time';

//引入用户相关的仓库
import useUserStore from '@/store/modules/user.ts';
let useStore = useUserStore();
let $router = useRouter();
let loading = ref(false);
let logRef = ref()
let loginForm = reactive({
    username: 'admin',
    password: '111111'
})

const login = async () => {
    await logRef.value.validate();
    //按钮加载的效果
    loading.value = true;


    try {
        await useStore.userLogin(loginForm);
        //登录成功路由跳转
        $router.push("/");
        ElNotification({
            type: 'success',
            message: '登录成功',
            title: `Hi,${getTime()}好`
        });
        loading.value = false;
    } catch (error) {
        //l
        loading.value = false;
        ElNotification({
            type: 'error',
            message: (error as Error).message
        })
    }
}
//定义表单校验贵州
const rules = {
    username: [
        { required: true, min: 5, max: 10, message: '账号长度最少5位', trigger: 'change' }
    ],
    password: [
        { required: true, min: 6, max: 15, message: '密码长度最少六位', trigger: 'change' }
    ]
}
</script>


<style scoped lang='scss'>
.login_container {
    width: 100%;
    height: 100vh;
    background: url("@/assets/images/background.jpg") no-repeat;
    background-size: cover;

    .login_from {
        position: relative;
        width: 80%;
        top: 30vh;
        background: url("@/assets/images/login_form.png") no-repeat;
        padding: 40px;
    }

    h1 {
        font-size: 40px;
        color: #fff;
    }

    h2 {
        margin: 20px 0;
        font-size: 20px;
        color: #fff;
    }

    .login_btn {
        width: 100%;
    }
}
</style>